﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using WarehouseManagement;
using WarehouseManagement.Header;

namespace WarehouseManagement.BO
{
class VatTuBO
{
	CommonDO cmDO = new CommonDO();

// Get VatTu Infomation
public string GetVatTuInfo(SqlConnection conn, ref DataTable tbVatTu, StructVatTu stVatTu )
{
    string cmd = SqlHeader.SP_GET_VAT_TU_INFO;

    string sCondiontion = createCondGetVatTu( stVatTu );

    if (sCondiontion == CommonHeader.BLANK)
    {
        return "Can not get material information because condition invalid!";
    }
    cmd = String.Concat(cmd, sCondiontion);

    return cmDO.getTable(conn, ref tbVatTu, cmd);
}

// Create condition to get VatTu infomation
private string createCondGetVatTu(StructVatTu stVatTu)
{
    string sCondition = CommonHeader.BLANK;

    if ( stVatTu.sID_1 != CommonHeader.BLANK )
    {
        sCondition = stVatTu.sID_1;
    }
    
    if (stVatTu.sID_2 != CommonHeader.BLANK)
    {
        sCondition = stVatTu.sID_2;
    }

    if (stVatTu.sID_3 != CommonHeader.BLANK)
    {
        sCondition = stVatTu.sID_3;
    }

    if (stVatTu.sID_45 != CommonHeader.BLANK)
    {
        sCondition = stVatTu.sID_45;
    }

    if (stVatTu.sID_67 != CommonHeader.BLANK)
    {
        sCondition = stVatTu.sID_67;
    }

    if (stVatTu.sID_89 != CommonHeader.BLANK)
    {
        sCondition = stVatTu.sID_89;
    }

    return String.Concat("'", sCondition, "%'");
}

// Get data for combobox
public string getComboboxData(  SqlConnection conn, 
                                ref DataTable tbCombo,
                                String sTableName )
{
   string cmd = "Select * from " + sTableName;
   return cmDO.getTable(conn, ref tbCombo, cmd);
}

// Update data by ID
public string UpdateRecordByID( SqlConnection conn, 
                                string sColUpdate ,
                                string sUpdateValue, 
                                string sIdValue)
{
    return  cmDO.UpdateRecordByID(conn,
                            SqlHeader.SP_UPDATE_TABLE_BY_ID,
                            VTHeader.TABLE_VAT_TU,
                            sColUpdate,
                            sUpdateValue,
                            VTHeader.TB_VT_VTID,
                            sIdValue);
}

// Insert new record when list has not record to refer
public String insertNewRecordGetID(    SqlConnection conn,
                                    string sNKID,
                                    string sShortID,
                                    string sGocID,
                                    string sNhomVTID,
                                    string sTenVatTu, 
                                    string sLVID, 
                                    string sNCC,
                                    string sTonKho, 
                                    string sNGang, 
                                    string sCao,
                                    string sSau,
                                    string sDonVi,
                                    string sGhiChu,
                                    ref string sNewID)                                    
{
	String cmd = "VatTu_add";

	cmd = String.Concat(cmd, " N'"	+ sNKID		+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sShortID	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sGocID	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sNhomVTID	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sTenVatTu	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sLVID		+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sNCC		+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sTonKho	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sNGang	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sCao		+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sSau		+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sDonVi	+ "'");
	cmd = String.Concat(cmd, ", N'"	+ sGhiChu	+ "'");

    return cmDO.InsertRecordGetID(conn, ref sNewID, cmd);
}

public string insertRecordGetID(    SqlConnection conn, 
                                    String sLastID, 
                                    ref String sNewID )
{
	String cmd = SqlHeader.SP_INSERT_VT;

    cmd = String.Concat(cmd, " '" + sLastID + "'");

    return cmDO.InsertRecordGetID(conn, ref sNewID, cmd);

}

public string deleteRecord( SqlConnection conn, string sID)
{
	return cmDO.DelereRecord(	conn, 
								VTHeader.TABLE_VAT_TU, 
								VTHeader.TB_VT_VTID, 
								sID );
}

public bool checkDuplicate(SqlConnection conn, string sNKID, string sNhomVT)
{
	DataTable tbTemp = new DataTable();
	
	string ret;
	
	string cmd = "Select VTID from VatTu where NKID = '"
				+ sNKID + "' and NhomVT = '" + sNhomVT + "'";
	ret = cmDO.getTable(conn, ref tbTemp, cmd);
	
	if (ret != CommonHeader.NO_ERROR )
	{
		return false;
	}
	else
	{
		if (tbTemp.Rows.Count > 0)
		{
			return false;		
		}
		return true;
	}
	
}

// End of class
}
// End of namespace
}
